package udditest.udditestartifact;

import java.rmi.RemoteException;

import org.apache.juddi.v3.client.ClassUtil;
import org.apache.juddi.v3.client.config.UDDIClientContainer;
import org.apache.juddi.v3.client.transport.Transport;
import org.apache.juddi.v3_service.JUDDIApiPortType;
import org.uddi.api_v3.AuthToken;
import org.uddi.api_v3.GetAuthToken;
import org.uddi.v3_service.DispositionReportFaultMessage;
import org.uddi.v3_service.UDDIPublicationPortType;
import org.uddi.v3_service.UDDISecurityPortType;

/**
 * Hello world!
 *
 */
public class App 
{
	//This variable is used to login in the JUDDI server
	private static UDDISecurityPortType security = null;
	//This variable is used to access all the API methods
	private static JUDDIApiPortType juddiApi = null;
    //This variable is used to publish (register) a service to the JUDDI server
    private static UDDIPublicationPortType publish = null;
    //This variable is used to cache user authToken, which is needed for later access to the IUDDI server.
    private static AuthToken myAuthToken = null;
	
    private static String username = "root";
    private static String password = "root";
    
    public static void main( String[] args )
    {
        System.out.println( "Starting..." );
        boolean flag = true;
        if (flag){        	
        	initialize();
        	System.out.println("Initialized");
        	try {
				authenticateToServer();
		        System.out.println (" User AUTHTOKEN = " + myAuthToken.getAuthInfo());
				
		        BusinessEntityManager manager = new BusinessEntityManager(publish, myAuthToken);
		        manager.createBusinessEntity();
		        System.out.println("Business entity created");
		        manager.createBusinessService("Group 6 Panos Gramoz Daniel");
		        System.out.println("Business service created");
			} catch (DispositionReportFaultMessage e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        } else {
        	initialize2();
        	System.out.println("Initalized");
        }
        
    }
    
    private static void authenticateToServer() throws DispositionReportFaultMessage, RemoteException {
    	GetAuthToken getAuthTokenMyPub = new GetAuthToken();
        getAuthTokenMyPub.setUserID(username);
        getAuthTokenMyPub.setCred(password);

        // Making API call that retrieves the authentication token for the user
        // You can use the group name and password assigned to you to access the JUDDIv3 server for this course.
        myAuthToken = security.getAuthToken(getAuthTokenMyPub);
	}

	public static void initialize(){
    	try {
    		String clazz = UDDIClientContainer.getUDDIClerkManager(null).getClientConfig().getUDDINode("default").getProxyTransport();
    		Class<?> transportClass = ClassUtil.forName(clazz, Transport.class);
    		if (transportClass!=null) {
               Transport transport = new BuildClient().buildClientManager();
               security = transport.getUDDISecurityService();
               juddiApi = transport.getJUDDIApiService();
               publish = transport.getUDDIPublishService();
            }
    	} catch (Exception e) {
    		e.printStackTrace();
	    }   
    }
    
    public static void initialize2(){
    	try {
    		String clazz = UDDIClientContainer.getUDDIClerkManager(null).getClientConfig().getUDDINode("default").getProxyTransport();
    		Class<?> transportClass = ClassUtil.forName(clazz, Transport.class);
    		if (transportClass!=null) {
               Transport transport = (Transport) transportClass.getConstructor(String.class).newInstance("default");   
               security = transport.getUDDISecurityService();
               juddiApi = transport.getJUDDIApiService();
               publish = transport.getUDDIPublishService();
            }
    	} catch (Exception e) {
    		e.printStackTrace();
	    }   
    }
    
}
